const tool = require('./utils/tool');

module.exports = {
  async getUser() {
    const { app, ctx } = this;
    try {
      const token = ctx.request.header.authorization;
      const decode = await app.jwt.verify(token, app.config.jwt.secret);
      let user = await ctx.model.SysUser.findOne({
        where: {
          user_name: decode.username
        },
        include: [ctx.model.SysRole,ctx.model.SysDept]
      });
      let _user = null;
      if(user){
        _user = user.toJSON()
        _user.dept = _user.sys_dept;
        _user.roles = _user.sys_roles;
        delete _user.sys_dept;
        delete _user.sys_roles;
        delete _user.dept_id;
        delete _user.password;
        delete _user.userType;
      }
      return _user;
    } catch (error) {
      return null;
    }
  },
  ...tool
}